package fakes

import (
	"archive/zip"
	"context"
	"go-caipu/pkg/plugins/storage"
)

type FakePluginStorage struct {
	Store        map[string]struct{}
	AddFunc      func(_ context.Context, pluginID string, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error)
	RegisterFunc func(_ context.Context, pluginID, pluginDir string) error
	RemoveFunc   func(_ context.Context, pluginID string) error
}

func (s *FakePluginStorage) Register(ctx context.Context, pluginID, pluginDir string) error {
	s.Store[pluginID] = struct{}{}
	if s.RegisterFunc != nil {
		return s.RegisterFunc(ctx, pluginID, pluginDir)
	}
	return nil
}

func (s *FakePluginStorage) Add(ctx context.Context, pluginID string, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
	s.Store[pluginID] = struct{}{}
	if s.AddFunc != nil {
		return s.AddFunc(ctx, pluginID, z)
	}
	return &storage.ExtractedPluginArchive{}, nil
}

func (s *FakePluginStorage) Remove(ctx context.Context, pluginID string) error {
	delete(s.Store, pluginID)
	if s.RemoveFunc != nil {
		return s.RemoveFunc(ctx, pluginID)
	}
	return nil
}
